#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>

#include <sys/ipc.h>
#include <sys/socket.h>

#include <arpa/inet.h>
#include <netinet/in.h>

#ifndef BUF_SIZE
#define BUF_SIZE 100
#endif

int main(int argc, char *argv[])
{
	struct sockaddr_in str_svr_addr, str_clt_addr;
	int s32_clt_sock, s32_ret;
	unsigned char u8_buf[BUF_SIZE];
	memset(u8_buf, 0, BUF_SIZE);
	
	assert(argc == 3);
	
	str_svr_addr.sin_family		 = AF_INET;
	str_svr_addr.sin_port		 = htons(argv[2]);
	str_svr_addr.sin_addr.s_addr = inet_addr(argv[1]);

	s32_clt_sock = socket(AF_INET,SOCK_STREAM,0);
	if( s32_clt_sock == -1 ) {
		perror("Client app: Unable to create socket");
		exit(1);
	}
	else {
		printf("Client app: Created socket\n");
	}
	str_clt_addr.sin_addr.s_addr = INADDR_ANY;
	s32_ret = connect(s32_clt_sock, (struct sockaddr *)&str_svr_addr,sizeof(str_svr_addr));
    if( s32_ret == -1 ) {
		perror("Client app: Unable to connect to server");
		exit(3);
    }
    else {
    	printf("Connected to server\n");
    }
    printf("Data from server:\n");
	while( (s32_ret = read(s32_clt_sock, &u8_buf, BUF_SIZE - 1)) != 0 ) {
		u8_buf[BUF_SIZE - 1] = 0;
		printf("%s",u8_buf);
	}
	close(s32_clt_sock);
	return 0;
}
		
	
	
	
	
	
	
	
	
	
	
	
	
	
	
